package com.rules.admin.unit.expect;

import com.gillion.grule.client.core.Context;
import com.gillion.grule.client.logger.LoggerAppenderHolder;

import com.rules.admin.utils.JSONUtils;
import org.apache.commons.lang3.StringUtils;
import org.mvel2.MVEL;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AssetExpect extends Expect {
   private static final Logger LOGGER = LoggerFactory.getLogger(AssetExpect.class);
   private String assertExpression;

   public ExpectMatcherResult expect(Context context) {
      if (context.getRtnValue() == null) {
         context.setRealRtnValue("");
      }

      context.setParams((List) JSONUtils.read(JSONUtils.write(context.getParams())));
      context.setRealRtnValue(JSONUtils.read(JSONUtils.write(context.getRtnValue())));
      boolean success = false;

      try {
         success = (Boolean)MVEL.eval(this.assertExpression, context);
      } catch (Exception var4) {
      }

      ExpectMatcherResult result = ExpectMatcherResult.ofAssert();
      if (success) {
         LoggerAppenderHolder.log("断言表达式匹配成功。", new Object[0]);
         result.setMatch(true);
         return result;
      } else {
         result.setMatch(false);
         if (StringUtils.isEmpty(this.errorMessage)) {
            LoggerAppenderHolder.log("开始匹配断言表达式：【{}】,数据的结果为：【{}】", new Object[]{this.assertExpression, context.getRtnValue()});
            result.setMessage("断言表达式：【" + this.assertExpression + "】不符合期望");
         } else {
            result.setMessage(this.errorMessage);
         }

         return result;
      }
   }

   public String getAssertExpression() {
      return this.assertExpression;
   }

   public void setAssertExpression(String assertExpression) {
      this.assertExpression = assertExpression;
   }

   public static void main(String[] args) {
      Map map = new HashMap();
      System.out.println(MVEL.eval("false=='wp'", map));
   }

   public String toString() {
      return "AssetExpect(assertExpression=" + this.getAssertExpression() + ")";
   }

   public boolean equals(Object o) {
      if (o == this) {
         return true;
      } else if (!(o instanceof AssetExpect)) {
         return false;
      } else {
         AssetExpect other = (AssetExpect)o;
         if (!other.canEqual(this)) {
            return false;
         } else {
            Object this$assertExpression = this.getAssertExpression();
            Object other$assertExpression = other.getAssertExpression();
            if (this$assertExpression == null) {
               if (other$assertExpression != null) {
                  return false;
               }
            } else if (!this$assertExpression.equals(other$assertExpression)) {
               return false;
            }

            return true;
         }
      }
   }

   protected boolean canEqual(Object other) {
      return other instanceof AssetExpect;
   }

   public int hashCode() {

      int result = 1;
      Object $assertExpression = this.getAssertExpression();
      result = result * 59 + ($assertExpression == null ? 43 : $assertExpression.hashCode());
      return result;
   }
}
